package com.hpf.cloud.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hpf.cloud.exception.CommonException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;

/**
 * @description: 返回结果
 * @datetime: 2020/11/15 20:43
 * @author: huangpengfei
 */
@Data
@ApiModel(value = "标准响应类")
public class CommonResult<T> implements Serializable {

    @ApiModelProperty(value = "响应业务状态")
    private boolean state;

    @ApiModelProperty(value = "响应消息")
    private String msg;

    @ApiModelProperty(value = "响应中的数据")
    private T data;

    @ApiModelProperty(value = "响应状态码 200 响应成功")
    private int code;

    @JsonIgnore
    @ApiModelProperty(value = "错误栈信息")
    private String errorStackInfo;

    public CommonResult(boolean state, String msg, T data, int code, String errorStackInfo) {
        super();
        this.state = state;
        this.msg = msg;
        this.data = data;
        this.code = code;
        this.errorStackInfo = errorStackInfo;
    }

    /**
     * 错误码信息+数据
     * @param resultCode
     * @param data
     */
    public CommonResult(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.state = resultCode.isState();
        this.msg = resultCode.getMessage();
        this.data = data;
    }
    
    public CommonResult() {
        super();
    }


    public static CommonResult resultCode(ResultCode resultCode) {
        return new CommonResult(resultCode, null);
    }

    public static CommonResult resultCode(ResultCode resultCode, Object object) {
        return new CommonResult(resultCode, object);
    }

    public static CommonResult ok(String msg, Object data) {
        if(data == null) {
            data = new ArrayList<>();
        }
        if(msg == null) {
            msg = "";
        }
        return new CommonResult(true, msg, data, ResultCode.SUCCESS.getCode(), null);
    }


    public static CommonResult ok(String msg) {
        return CommonResult.ok(msg, null);
    }

    public static CommonResult ok(Object data) {
        return CommonResult.ok(null, data);
    }


    public static CommonResult ok() {
        return CommonResult.ok(null);
    }

    public static CommonResult failed(String msg) {
        if(msg == null) {
            msg = "";
        }
        return new CommonResult(false, msg, new ArrayList<Object>(), ResultCode.FAIL.getCode(), msg);
    }


    public static CommonResult failed(String msg, Exception ex) {
        if(msg == null) {
            msg = "";
        }
        if(ex == null) {
            return new CommonResult(false, msg, new ArrayList<>(), ResultCode.FAIL.getCode(), msg);
        }else {
            return new CommonResult(false, msg, new ArrayList<>(), ResultCode.FAIL.getCode(), printStackTraceToString(ex));
        }
    }
    /**
     * 验证不通过
     * @param msg
     * @return
     */
    public static CommonResult failedCheck(String msg) {
        if(msg == null) {
            msg = "";
        }
        return new CommonResult(false, msg, new ArrayList<Object>(), ResultCode.FAIL.getCode(), msg);
    }
    
    public static CommonResult failedCheck(String msg, Object data) {
        if(msg == null) {
            msg = "";
        }
        if(data == null) {
            data = new ArrayList<Object>();
        }
        return new CommonResult(false, msg, data, ResultCode.SUCCESS.getCode(), null);
    }

    public static CommonResult failed(Exception ex) {
        if (ex instanceof CommonException) {
            return failed(ex.getMessage(), ex);
        } else if (ex.getCause() instanceof CommonException) {
            return failed(ex.getCause().getMessage(), ex);
        } else if (ex.getCause() != null && ex.getCause().getCause() instanceof CommonException){
            return failed(ex.getCause().getCause().getMessage(), ex);
        }
        return failed(ResultCode.SERVER_ERROR.getMessage(), ex);
    }
    

    public static String printStackTraceToString(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }

    /**
     *  校验后台返回CommonResult是否正常返回数据
     * @param commonResult
     * @return boolean
     */
    public static boolean standardResultBoolean(CommonResult commonResult) {
        boolean flag = false;
        if (commonResult != null && commonResult.getCode() == ResultCode.SUCCESS.getCode() && commonResult.isState()
                && commonResult.getData() != null) {
            flag = true;
        }
        return flag;
    }
}
